function mouseButtonRelease(hObject,eventdata)
%controls what to do when mouse moves while a button is released

% This file is part of CISNE.
%
%     CISNE is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     any later version.
%
%     CISNE is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
%
%     You should have received a copy of the GNU General Public License
%     along with CISNE.  If not, see <http://www.gnu.org/licenses/>.

% Copyright 2011. Jose Maria Garcia-Valdecasas Bernal
% 21-Oct-09 ver. 1.0 (splitted from CISNE.m).

handles=guidata(hObject);
grid=handles.TreeWidget.currentNode();
mouseclick=get(handles.figure1,'SelectionType');
if strcmp(mouseclick,'normal')&handles.mouseButtonDown %#ok<AND2>
    if (strcmp(handles.gridAction,'zoomArea'))
        [x,y]=handles.DisplayWidget.getTempRect();
        x=[min(x) max(x)];
        y=[min(y) max(y)];
        handles.DisplayWidget.zoomArea(x,y);
    elseif (strcmp(handles.gridAction,'addCPoint'))
        handles.TreeWidget.setValue('X',handles.xp0,grid);
        handles.TreeWidget.setValue('Y',handles.yp0,grid);
        handles.DisplayWidget.drawCPoint(grid,handles.xp0,handles.yp0);
    
    elseif (strcmp(handles.gridAction,'movePoint'))
        p=handles.DisplayWidget.currentPoint();
        handles.TreeWidget.setValue('X',p(1,1),grid);
        handles.TreeWidget.setValue('Y',p(1,2),grid);
        handles.DisplayWidget.drawCPoint(grid,p(1,1),p(1,2));
        handles.DisplayWidget.resetTempPoint();
        handles.DisplayWidget.deactivateCPoint(); %deselect it
    else %editing a computational grid
        p=handles.DisplayWidget.currentPoint();
        if (handles.xp0 == p(1,1))&&(handles.yp0==p(1,2))
            handles.TreeWidget.setValue('X0',0,grid);
            handles.TreeWidget.setValue('Y0',0,grid);
            handles.TreeWidget.setValue('X Length',0,grid);
            handles.TreeWidget.setValue('Y Length',0,grid);
        else
            [x,y]=handles.DisplayWidget.getTempRect();
            d=sqrt(x.^2.+y.^2);
            [md,ind]=min(d);
            %FIXME: redondear las coordenadas del pto inicial si x=[0 1]??
            handles.TreeWidget.setValue('X0',round(x(ind)),grid);
            handles.TreeWidget.setValue('Y0',round(y(ind)),grid);
            handles.DisplayWidget.drawCGrid(grid,x,y);
        end
		handles.DisplayWidget.hideGridCorners();
    end
    handles.DisplayWidget.resetTempRect();
    handles.DisplayWidget.setAction('');
    handles.mouseButtonDown=0;
    handles.gridAction=[];
end
set(handles.figure1,'Pointer','arrow');
guidata(hObject,handles);

